# -*- encoding: utf-8 -*-
# @File    : ControlList.py
# @Date    : 2020/12/06
# @Author  : 韩林新

"""
    Class
        ControlList
        ControlItems
"""
from .Control import Control
from .Control import CtrlId
from .ControlType import CTRL_TYPE
from .config import debug

class ControlList(object):
    """
    记录控件列表

    Attributs
        data:字典，{ type1 :{ name1: ctrl1, name2: ctrl2 }, type2 :{{name1: ctrl1,name2: ctrl2}} }
        cnt:字典，{ type1: cnt1, type2: cnt2}
    """

    def __init__(self):
        self.data = {}
        self.cnt = {}
        self.sum = 0

        for ctrltype in CTRL_TYPE:
            self.data[ctrltype] = {}
            self.cnt[ctrltype] = 0

    def __getitem__(self, key):
        return self.data[key]

    def add_control(self, ctrl):
        """
        新增控件
        """
        if self.find(ctrl):
            old_left = self[ctrl.type][ctrl.name].rect.left
            old_top = self[ctrl.type][ctrl.name].rect.top
            old_right = self[ctrl.type][ctrl.name].rect.right
            old_bottom = self[ctrl.type][ctrl.name].rect.bottom

            if old_left != ctrl.rect.left or old_top != ctrl.rect.top or old_right != ctrl.rect.right or old_bottom != ctrl.rect.bottom:
                self[ctrl.type][ctrl.name] = ctrl   # update
                # ctrl.log()
            return
        else:
            # ctrl.log()
            self.sum += 1
            self.data[ctrl.type][ctrl.name] = ctrl
            self.cnt[ctrl.type] += 1

    def types(self):
        return self.data.keys()

    def show_type(self, ctrltype):
        """
        显示特定类型控件

        Parameters
            ctrltype: 控件类型
        """
        for ctrlname in self.data[ctrltype].keys():
            print(self[ctrltype][ctrlname])

    def show(self):
        """
        显示所有控件
        """
        for ctrltype in self.data.keys():
            self.show_type(ctrltype)

    def find(self, ctrl):
        """
        查找ctrl是否存在

        Parameters
            ctrl： 待查找控件

        Returns
            True or False
        """
        if ctrl.type in self.types() and ctrl.name in self[ctrl.type].keys():
            return True
        else:
            return False

    def find_by_name(self, type: str, name: str) -> bool:
        """
        查找ctrl是否存在

        Parameters
            ctrl： 待查找控件

        Returns
            True or False
        """
        if type in self.types() and name in self[type].keys():
            return True
        else:
            return False

    def find_by_id(self, ctrl_id: CtrlId) -> Control:
        """
        根据控件id查找控件

        Parameters
            ctrl_id 控件id
        Returns
            Control对象
        """
        if ctrl_id.type in self.types() and ctrl_id.name in self[ctrl_id.type].keys():
            return self[ctrl_id.type][ctrl_id.name]
        else:
            return None

    def has_ctrl(self, ctrl_id: CtrlId) -> bool:
        """
        根据控件id查询是否存在

        Parameters
            ctrl_id  控件id
        Returns
            True/False
        """
        # print("ctrl_id.type = {},name = {},type = {}".format(ctrl_id.type,ctrl_id.name,type(ctrl_id.name)))
        # print("flag1 = {}".format(ctrl_id.type in self.types()))# and ctrl_id.name in self[ctrl_id.type].keys()))
        # print("types = {}".format(self.types()))
        # if ctrl_id.type in self.types():
        #     names = self[ctrl_id.type].keys()
        #     print("names = {}".format(names))
        #     print("flag2 = {}".format(ctrl_id.name in names))
        #     for name in names:
        #         print("name = {},type = {}".format(name,type(name)))
        #         if name == ctrl_id.name:
        #             print("OK!*******")
        if ctrl_id.type in self.types() and ctrl_id.name in self[ctrl_id.type].keys():
            return True
        else:
            return False

    def union(self, ctrl_list):
        """
        合并其他ControlList

        Parameters
            ctrl_list 被合并的ControlList
        Returns
            None
        """
        for ctype in CTRL_TYPE:
            for ctrl_name in ctrl_list[ctype]:
                self.add_control(ctrl_list[ctype][ctrl_name])
